home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / media / objects / converte / 3d2-to-o.c next >
Encoding:
C/C++ Source or Header  |  1994-10-03  |  6.6 KB  |  258 lines

  1. /* 
  2.  
  3.    simple 3D2-to-obj converter   -   6/19/93 by Francisco X DeJesus
  4.  
  5.    dejesus@c3ot.saic.com     -    dejesus@avalon.chinalake.navy.mil
  6.  
  7.    (written under SunOS 4.1.3... tweaks may be necessary for the
  8.     date/time stuff to work, but it is not crucial, so they can be
  9.     commented out and a fake date hard-coded if neccesary).
  10.  
  11.    This program is provided "as-is" (ie: use it at your own risk!).
  12.    Permission is hereby granted to redistribute this program freely,
  13.    provided that this header is left unmodified. If you find any bugs or
  14.    modify the program to improve it in any way, please let me know...
  15.  
  16.    If you have any interesting 3D objects/models, in any format, please
  17.    upload them to the anonymous ftp archive avalon.chinalake.navy.mil!!!
  18.  
  19. */
  20.  
  21. #include <stdio.h>
  22. #include <sys/types.h>
  23. #include <sys/timeb.h>
  24. #include <sys/time.h>
  25. #include <strings.h>
  26. #include <string.h>
  27.  
  28. static char *months[]=
  29.    {
  30.    "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  31.    };
  32.  
  33. static char *days[]=
  34.    {
  35.    "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
  36.    };
  37.  
  38. main(argc,argv)
  39. int argc;
  40. char *argv[];
  41. {
  42.     FILE *fpn,*fpo;
  43.     struct timeval now;
  44.     struct timezone zone;
  45.     time_t then;
  46.     struct tm *t;
  47.     char infilename[40];
  48.     char outfilename[40];
  49.     char oname[9];
  50.     char cx, cy, cz;
  51.     unsigned char discard[1000], objname[10];
  52.     unsigned short int objects[2], tobj, ofaces[2], overtices[2];
  53.     unsigned short int f1, f2, f3, of1[2], of2[2], of3[2];
  54.     short int vx[2], vy[2], vz[2];
  55.     unsigned int vertices, faces;
  56.     unsigned int i, v, f;
  57.     unsigned long int vtotal, ftotal, cvtotal;
  58.     float x, y, z;
  59.  
  60.     if (argc == 1)
  61.         {
  62.             printf("3D2 file to be converted? ");
  63.             scanf("%s",infilename);
  64.             printf("Obj file to output? ");
  65.             scanf("%s",outfilename);
  66.             if ((fpn = fopen(infilename,"r")) == 0) {
  67.                 printf ("file does not exist: %s\n", infilename);
  68.                 exit ();
  69.                 }
  70.             if ((fpo = fopen(outfilename,"w")) == 0) {
  71.                 printf ("file cannot be created: %s\n", outfilename);
  72.                 exit ();
  73.                 }
  74.         }
  75.     else if (argc == 2)
  76.         {
  77.             if (strcmp (argv[1], "-h") == 0) {
  78.                 printf("usage:\n");
  79.                 printf("        tpoly2obj [inputfile.tpoly [outputfile.obj]]\n");
  80.                 printf("Please use full filenames. If files are not speficied on the\n");
  81.                 printf("command line, the program will prompt you for them.\n");
  82.                 exit();
  83.                 }
  84.             printf("Obj file to output? ");
  85.             scanf("%s",outfilename);
  86.             if ((fpn = fopen(argv[1],"r")) == 0) {
  87.                 printf ("file does not exist: %s\n", argv[1]);
  88.                 exit ();
  89.                 }
  90.             if ((fpo = fopen(outfilename,"w")) == 0) {
  91.                 printf ("file cannot be created: %s\n", outfilename);
  92.                 exit ();
  93.                 }
  94.         }
  95.     else
  96.         {
  97.             if ((fpn = fopen(argv[1],"r")) == 0) {
  98.                 printf ("file does not exist: %s\n", argv[1]);
  99.                 exit ();
  100.                 }
  101.             if ((fpo = fopen(argv[2],"w")) == 0) {
  102.                 printf ("file cannot be created: %s\n", argv[2]);
  103.                 exit ();
  104.                 }
  105.         }
  106.  
  107. /* convert! */
  108.  
  109.         if (gettimeofday(&now,&zone)<0)
  110.                 perror("gettimeofday failed"), exit(2);
  111.  
  112.         if ((t=localtime(&now))==NULL)
  113.                 perror("localtime failed"), exit(3);
  114.  
  115.         if (t->tm_mday > 9)
  116.                 fprintf(fpo,"# %s %s %2.2d %2.2d:%2.2d:%2.2d %4.4d\n",
  117.                        days[t->tm_wday],months[t->tm_mon],t->tm_mday,
  118.                        t->tm_hour,t->tm_min,t->tm_sec,
  119.                        (1900+t->tm_year));
  120.         if (t->tm_mday < 10)
  121.                 fprintf(fpo,"# %s %s  %d %2.2d:%2.2d:%2.2d %4.4d\n",
  122.                         days[t->tm_wday],months[t->tm_mon],t->tm_mday,
  123.                         t->tm_hour,t->tm_min,t->tm_sec,
  124.                         (1900+t->tm_year));
  125.  
  126.     fprintf(fpo,"#\n");
  127.     fprintf(fpo,"# Object converted by 3D2-to-obj\n");
  128.     fprintf(fpo,"#\n\n");
  129.  
  130.     printf("Starting conversion...\n\n");
  131.  
  132.  
  133.     fprintf(fpo,"g\n");
  134.  
  135.  
  136. /* read header ... discard everything but the number of objects */
  137.  
  138.     fread(discard,1,2,fpn);
  139.  
  140.     fread(objects,1,2,fpn);
  141.  
  142.     fread(discard,1,252,fpn);   /* should be 316, -64??? */
  143.  
  144.     tobj=objects[0];
  145.     printf("%d objects in .3d2 file...\n\n",tobj);
  146.  
  147. /* main loop one, once for each object - write out vertex list*/
  148.  
  149.     vtotal=0;
  150.     ftotal=0;
  151.     cvtotal=0;
  152.     for (i=1;i<=tobj;i++) {
  153.          printf("   object #%d\n",i);
  154.  
  155.          fread(objname,1,9,fpn);
  156.  
  157.      printf("      object name: %s\n",objname);
  158.  
  159. /* vertex loop - write out */
  160.  
  161.      fread(overtices,1,2,fpn);
  162.      vertices=((overtices[1]>>8)+overtices[0]);
  163.      vtotal=vtotal+vertices;
  164.  
  165.      printf("      vertices: %d\n",vertices);
  166.  
  167.      for (v=1;v<=vertices;v++) {
  168.  
  169.           fread(vx,1,2,fpn);
  170.           fread(vy,1,2,fpn);
  171.           fread(vz,1,2,fpn);
  172.  
  173.           x=((float)vx[0])/100;
  174.           y=((float)vy[0])/100;
  175.           z=((float)vz[0])/100;
  176.  
  177.           fprintf(fpo,"v %f %f %f\n",x,y,z);
  178.      }
  179.  
  180. /* face loop - discard */
  181.  
  182.      fread(ofaces,1,2,fpn);
  183.      faces=((ofaces[1]>>8)+ofaces[0]);
  184.      ftotal=ftotal+faces;
  185.  
  186.      printf("      faces: %d\n",faces);
  187.  
  188.      for (f=1;f<=faces;f++) {
  189.           fread(discard,1,8,fpn);
  190.      }
  191.  
  192.          printf("\n");
  193.     }
  194.  
  195. /* end of main loop one */
  196.  
  197.  
  198.     printf("total: %d vertices , %d faces\n",vtotal,ftotal);
  199.  
  200.     fprintf(fpo,"# %d vertices\n\n",vtotal);
  201.  
  202.     fprintf(fpo,"# 0 vertex parms\n\n");
  203.  
  204.     fprintf(fpo,"# 0 texture vertices\n\n");
  205.  
  206.     fprintf(fpo,"# 0 normals\n\n");
  207.  
  208.     rewind(fpn);
  209.  
  210. /* discard header altogether the second time around... */
  211.  
  212.     fread(discard,1,256,fpn);
  213.  
  214.  
  215. /* main loop two, once for each object - write out faces/group list*/
  216.  
  217.     for (i=1;i<=tobj;i++) {
  218.          fread(objname,1,9,fpn);
  219.  
  220.      fprintf(fpo,"g %s\n",objname);
  221.  
  222. /* vertex loop- discard  */
  223.  
  224.      fread(overtices,1,2,fpn);
  225.      vertices=((overtices[1]>>8)+overtices[0]);
  226.  
  227.      for (v=1;v<=vertices;v++) {
  228.           fread(discard,1,6,fpn);
  229.      }
  230.  
  231. /* face loop - write to file */
  232.  
  233.      fread(ofaces,1,2,fpn);
  234.      faces=((ofaces[1]>>8)+ofaces[0]);
  235.  
  236.      for (f=1;f<=faces;f++) {
  237.           fread(of1,1,2,fpn);
  238.           fread(of2,1,2,fpn);
  239.           fread(of3,1,2,fpn);
  240.           f1=(((of1[1]>>8)+of1[0])+1+cvtotal);
  241.           f2=(((of2[1]>>8)+of2[0])+1+cvtotal);
  242.           f3=(((of3[1]>>8)+of3[0])+1+cvtotal);
  243.           fread(discard,1,2,fpn);
  244.           fprintf(fpo,"f %d %d %d\n",f1,f2,f3);
  245.  
  246.      }
  247.      cvtotal=cvtotal+vertices;
  248.     }
  249.  
  250. /* end of main loop two */
  251.  
  252.  
  253.     fprintf(fpo,"# %d elements\n",ftotal);
  254.  
  255.     fclose(fpn);
  256.     fclose(fpo);
  257. }
  258.